package com.atgouwu.mall.cart.interceptor;

import com.atgouwu.mall.common.utils.JwtUtils;
import com.atgouwu.mall.cart.to.UserInfoTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class MyHandlerInterceptor implements HandlerInterceptor {

    public static ThreadLocal<UserInfoTo> toThreadLocal = new ThreadLocal<>(); //jdk提供,同一线程共享数据

//    //目标方法执行前:SpringSession判断用户登录状态,设置登录用户id,临时用户key
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        UserInfoTo userInfoTo = new UserInfoTo();
//        HttpSession session = request.getSession();
//        MemberResponseVo memberResponseVo = (MemberResponseVo) session.getAttribute(LOGIN_USER); //获得当前登录用户的信息
//        if (memberResponseVo != null) { //用户登录了
//            userInfoTo.setUserId(memberResponseVo.getId()); //登录用户id
//        }
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null && cookies.length > 0) {
//            for (Cookie cookie : cookies) {
//                if (cookie.getName().equals(TEMP_USER_COOKIE_NAME)) {
//                    userInfoTo.setUserKey(cookie.getValue()); //临时用户key(登录与未登录都有临时用户)
//                    userInfoTo.setTempUser(true); //标记为已是临时用户
//                }
//            }
//        }
//        if (StringUtils.isEmpty(userInfoTo.getUserKey())) { //第一次没有临时用户分配一个临时用户
//            String uuid = UUID.randomUUID().toString();
//            userInfoTo.setUserKey(uuid);
//        }
//        toThreadLocal.set(userInfoTo); //目标方法执行之前,将用户信息放入同一线程共享数据
//        return true;
//    }
//
//    //目标方法执行后:分配临时用户给浏览器保存
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        UserInfoTo userInfoTo = toThreadLocal.get(); //获取当前用户
//        if (!userInfoTo.getTempUser()) { //没有临时用户保存一个临时用户
//            Cookie cookie = new Cookie(TEMP_USER_COOKIE_NAME, userInfoTo.getUserKey()); //创建一个cookie
//            cookie.setDomain("mall.com"); //扩大作用域
//            cookie.setMaxAge(TEMP_USER_COOKIE_TIMEOUT); //设置过期时间一个月
//            response.addCookie(cookie);
//        }
//    }

    //目标方法执行前:JWT判断用户登录状态(设置登录用户id,临时用户key)
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfoTo userInfoTo = new UserInfoTo();
        String userTempId = request.getHeader("userTempId"); //临时key(登录和未登录都有)
        if(!StringUtils.isEmpty(userTempId)){
            userInfoTo.setUserKey(userTempId);
        }
        String token = request.getHeader("token");
        if (JwtUtils.checkToken(token)) { //已登录
            userInfoTo.setUserId(JwtUtils.getUserId(token)); //登录用户id
        } else {
            String tokens = request.getParameter("token"); //订单feign调用购物车
            if (JwtUtils.checkToken(tokens)) {
                userInfoTo.setUserId(JwtUtils.getUserId(tokens));
            }
        }
        toThreadLocal.set(userInfoTo); //将用户信息放入同一线程共享
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
